package service;

import dao.ListDao;
import dao.MeetingDao;
import entity.Dept;
import entity.Meeting;
import entity.PageEntity;
import entity.User;

import java.util.List;

public class MeetingService {
    MeetingDao meetingDao = new MeetingDao();

    /**
     * 没有会议状态查询会议
     * @param title
     * @param currPage
     * @return
     */
    public PageEntity<Meeting> findPage(String title, Integer currPage) {
        PageEntity<Meeting> meetingPageEntity = new PageEntity<>();

        //start页面
        Integer startPage = (currPage - 1) * meetingPageEntity.getPageSize();
        Integer endPage = meetingPageEntity.getPageSize();

        //总的记录数
        Integer count = meetingDao.findCount(title);

        //总页数
        Integer totalPage = count%endPage==0?count/endPage: count/endPage+1;

        List<Meeting> meetingList =  meetingDao.findPage(title,startPage,endPage);
        meetingPageEntity.setData(meetingList);
        meetingPageEntity.setCurrPage(currPage);
        meetingPageEntity.setTotalPage(totalPage);
        meetingPageEntity.setTotalCount(count);
        return meetingPageEntity;
    }

    /**
     *  有会议状态查询会议
     * @param title
     * @param currPage
     * @param statusL
     * @return
     */
    public PageEntity<Meeting> findPageS(String title, Integer currPage, Long statusL) {
        PageEntity<Meeting> meetingPageEntity = new PageEntity<>();

        //start页面
        Integer startPage = (currPage - 1) * meetingPageEntity.getPageSize();
        Integer endPage = meetingPageEntity.getPageSize();

        //总的记录数
        Integer count = meetingDao.findCountS(title,statusL);

        //总页数
        Integer totalPage = count%endPage==0?count/endPage: count/endPage+1;

        List<Meeting> meetingList =  meetingDao.findPageS(title,statusL,startPage,endPage);
        meetingPageEntity.setData(meetingList);
        meetingPageEntity.setCurrPage(currPage);
        meetingPageEntity.setTotalPage(totalPage);
        meetingPageEntity.setTotalCount(count);
        return meetingPageEntity;
    }

    /**
     * 根据部门id查部门名称
     * @param deptId
     * @return
     */
    public String findBydeptName(Long deptId) {
        return meetingDao.findBydeptName(deptId);
    }

    /**
     * 添加会议
     * @param meeting
     */
    public void insertMeeting(Meeting meeting) {
        meetingDao.insertMeeting(meeting);
    }

    /**
     * 查询所有的部门
     * @return
     */
    public List<Dept> findBydept() {
        return meetingDao.findBydept();
    }

    /**
     * 根据部门查询下面对应的员工
     * @param deptId
     * @return
     */
    public List<User> findByUser(Integer deptId) {
        return meetingDao.findByUser(deptId);
    }

    /**
     * 查询会议详情
     * @param meetingId
     * @return
     */
    public Meeting findById(long meetingId) {
        return meetingDao.findfindById(meetingId);
    }

    /**
     * 查询会议的实到人数
     * @param meetingId
     * @return
     */
    public Integer countJoinMeetingByMeetingId(Long meetingId) {
        return meetingDao.countJoinMeetingByMeetingId(meetingId);
    }

    /**
     * 查询指定用户是否已经参加这次会议
     * @param userId
     * @param meetingId
     * @return
     */
    public Boolean isJoinMeeting(Long userId, Long meetingId) {
        Integer count = meetingDao.isJoinMeeting(userId, meetingId);

        return count == 0 ? false : true;
    }
    /**
     * 参加会议
     *
     * @param userId
     * @param meetingId
     */
    public void joinMeeting(Long userId, long meetingId) {

        meetingDao.joinMeeting(userId, meetingId);

    }

    /**
     * 取消会议
     * @param userId
     * @param meetingId
     */
    public void unJoinMeeting(Long userId, long meetingId) {
        meetingDao.unJoinMeeting(userId, meetingId);
    }

    /**
     * 查询用户是否参加了这次会议
     * @param id
     * @param meetingId
     * @return
     */
    public Integer countByUserIdAndMeetingId(Long id, Long meetingId) {
        return meetingDao.countByUserIdAndMeetingId(id,meetingId);
    }
}
